<?php
namespace api\home\controller;

use think\Db;
use think\Validate;
use cmf\controller\RestUserBaseController;
use app\common\model\AppUserModel;
use app\common\logic\Img;
use app\common\logic\home\Lesson;
//用户信息
class AppUserController extends RestUserBaseController
{
    /**
     * [获取用户信息]
     * @param [type] $param.id [用户id]
     */
    public function index(){
        $data = AppUserModel::get($this->userId);
        if(empty($data['realName']) || empty($data['schoolName']) || empty($data['grad']) || empty($data['classes']) || empty($data['entranceTime'])){
            $this->error('请完善信息',1);
        }else if(empty($data['avater'])){
            $this->error('请上传头像',2);
        }else{
            $this->success('');
        }
    }
    /**
     * [获取用户信息]
     * @return [type] [description]
     */
    public function getInfo(){
        $data = AppUserModel::get($this->userId);
        if(!empty($data)){
            $this->success('成功',$data->visible(['realName','nickname','header'])->toArray());
        }else{
            $this->error('失败');
        }
    }
    /**
     * [上传图像]
     * @return [type] [description]
     */
    public function avatar(Img $Img){
        $param = $this->request->param();
        $res = $Img->index($param['img']);//头像
        if($res['code'] == 1){
            $this->success('成功',$res['data']);
            
        }else{
            $this->error('上传失败');
        }
    }
    //保存自拍照到用户
    public function addAvatar(Lesson $Lesson){
        //$Lesson->rem_2($this->userId);die;
        $param = $this->request->param();
        $rs = AppUserModel::where('id',$this->userId)->update(['avater'=>$param['avater']]);
        if($rs){
            $Lesson->rem_2($this->userId);
            $this->success('成功');
        }else{
            $this->success('失败');
        }
    }

}
